/* Copyright (c) 2012, Roberto Javier Godoy.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Please contact Roberto Javier Godoy at <rjgodoy@fich.unl.edu.ar>,
 * <mail@rjgodoy.com.ar> if you need additional information or have any
 * questions.
 */
package ar.com.rjgodoy.sgml.sax;

import org.xml.sax.Locator;

/**Implementation of {@code org.xml.sax.Locator}.
 *
 * @author Roberto Javier Godoy
 */
class LocatorImpl implements Locator {

	private long ptr;

	LocatorImpl(long ptr) {
		this.ptr = ptr;
	}

	@Override
	public int getColumnNumber() {
		if (ptr==0) return -1;
		return getColumnNumber0(ptr);
	}

	@Override
	public int getLineNumber() {
		if (ptr==0) return -1;
		return getLineNumber0(ptr);
	}

	@Override
	public String getPublicId() {
		return null;
	}

	@Override
	public String getSystemId() {
		if (ptr==0) return null;
		String systemId = getSystemId0(ptr);
		if (systemId.length()==0) return null;
		return systemId;
	}

	private static native int getColumnNumber0(long ptr);

	private static native int getLineNumber0(long ptr);

	private static native String getSystemId0(long ptr);

	 void disposeLocator() {
		 this.ptr=0;
	 }

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		String systemId=getSystemId();
		int lineNumber = getLineNumber();
		int columnNumber = getColumnNumber();
		if (systemId!=null) sb.append(systemId);
		if (lineNumber>=0) {
			sb.append(':').append(lineNumber);
			if (columnNumber>=0) sb.append(':').append(columnNumber);
		}
		if (sb.length()==0) sb.append("Unknown location");
		return sb.toString();
	}

}
